package com.czl.module_storehouse.activity.allocate.details;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;

import com.czl.base.util.SpHelper;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.FooterCommonRemarkBinding;
import com.czl.module_storehouse.databinding.HeaderAllocatePostOutBinding;
import com.czl.module_storehouse.databinding.LayoutRecyclerViewBinding;
import com.czl.module_service.system.Constants;
import com.czl.module_storehouse.adapter.AllocateDetailsAdapter;
import com.czl.module_storehouse.bean.AllocateBean;
import com.czl.module_storehouse.event.AllocateEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * @author xubin
 */
public class AllocateDetailsActivity extends BaseActivity<LayoutRecyclerViewBinding> {

    private HeaderAllocatePostOutBinding mHeaderBinding;
    private FooterCommonRemarkBinding mFooterBinding;
    private AllocateDetailsAdapter mAdapter;

    @Override
    protected LayoutRecyclerViewBinding getBinding(LayoutInflater layoutInflater) {
        return LayoutRecyclerViewBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("调拨详情");
        mAdapter = new AllocateDetailsAdapter(R.layout.item_allocate_details, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);
        mHeaderBinding = HeaderAllocatePostOutBinding.inflate(getLayoutInflater());
        mAdapter.addHeaderView(mHeaderBinding.getRoot());
        mFooterBinding = FooterCommonRemarkBinding.inflate(getLayoutInflater());
        mAdapter.addFooterView(mFooterBinding.getRoot());
        EventBus.getDefault().register(this);
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(AllocateBean allocateBean) {
        mHeaderBinding.clHeaderInfo.ivHeader.setImageResource(R.mipmap.icn_idx_lingyong);
        mHeaderBinding.clHeaderInfo.tvHeaderTitle.setText(allocateBean.getMoveName());
        mHeaderBinding.clHeaderInfo.tvHeaderCode.setText("调拨单：" + (allocateBean.getMoveCode() == null ? "" : allocateBean.getMoveCode()));
        mHeaderBinding.clHeaderInfo.tvHeaderDate.setText(allocateBean.getGmtCreate());
        mHeaderBinding.tvHeaderCompany.setText("申请公司：" + (allocateBean.getApplyCompanyName() == null ? "" : allocateBean.getApplyCompanyName()));
        mHeaderBinding.tvHeaderDepartment.setText("申请部门：" + (allocateBean.getApplyPersonDepartment() == null ? "" : allocateBean.getApplyPersonDepartment()));
        mHeaderBinding.tvStoreOut.setText("调出仓库：" + (allocateBean.getMoveStorehouseName() == null ? "" : allocateBean.getMoveStorehouseName()));
        mHeaderBinding.tvStoreIn.setText("调入仓库：" + (allocateBean.getInStorehouseName() == null ? "" : allocateBean.getInStorehouseName()));
        mHeaderBinding.tvReason.setText("调拨原因：" + (allocateBean.getApplyComment() == null ? "" : allocateBean.getApplyComment()));
        mHeaderBinding.tvOutCompany.setText("调出公司：" + (allocateBean.getMoveCompanyName() == null ? "" : allocateBean.getMoveCompanyName()));
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(AllocateEvent event) {
        AllocateBean allocateBean = event.getAllocateBean();
        if (allocateBean == null) {
            return;
        }
        mAdapter.setVisible(allocateBean.getAllCompleteBol());
        initHeaderView(allocateBean);
        mFooterBinding.etRemark.setEnabled(false);
        mFooterBinding.etRemark.setText(allocateBean.getMoveComment(SpHelper.INSTANCE.decodeInt(Constants.SpKey.STOREHOUSE_ID)));
        mAdapter.setList(allocateBean.getSortList());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
    }
}
